package com.otp.controller;

import com.otp.pojo.dto.UserDto;
import com.otp.pojo.entity.User;
import com.otp.pojo.request.user.*;
import com.otp.util.ResponseMessage;
import com.otp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseMessage<UserDto> login(@RequestBody UserLoginRequest request) {
        return ResponseMessage.success(userService.login(request)
                .convert());
    }

    @PostMapping("/register")
    public ResponseMessage<UserDto> register(@RequestBody UserRegisterRequest request) {
        return ResponseMessage.success(userService.register(request)
                .convert());
    }

    @PutMapping
    public ResponseMessage<UserDto> update(@RequestBody UserUpdateRequest request) {
        return ResponseMessage.success(userService.update(request)
                .convert());
    }

    @DeleteMapping
    public ResponseMessage<Void> delete(@RequestParam Long id) {
        return ResponseMessage.success(userService.delete(id));
    }

    @GetMapping("/{id}")
    public ResponseMessage<UserDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(userService.getById(id)
                .convert());
    }

    @GetMapping("/all")
    public ResponseMessage<List<UserDto>> getAll() {
        return ResponseMessage.success(userService.getAll()
                .stream()
                .map(User::convert)
                .toList());
    }
}